package com.qianfeng.smartplatform.websocket;/*
 *@Description:
 *@author:赵健
 *@Date:zjzzz 2022/9/13 19:47
 */

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.PongMessage;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.handler.TextWebSocketHandler;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

//@Component
//@EnableWebSocket//开启websocket
public class TestWebscoket extends TextWebSocketHandler{
    @Autowired
    private ObjectMapper objectMapper;

    //保存客户端的map

    private static Map<String, WebSocketSession> allClients = new ConcurrentHashMap<>();
    public void afterConnectionEstablished(WebSocketSession session) throws Exception {
        System.out.println("TestWebscoket中的方法afterConnectionEstablished 执行了--->");
        String path = session.getUri().getPath();
        //我们约定最后一个/后面的就是用户名
        String userName = path.substring(path.lastIndexOf("/") + 1);
        //我们把数据保存到属性中,方便后面获取
        session.getAttributes().put("name", userName);
        //保存了会话
        allClients.put(userName, session);
    }

    protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
        String payload = message.getPayload();
        System.err.println("TestWebscoket中的方法handleTextMessage 执行了--->" + payload);
        //找到目标收消息的人的session,在某个地方一定保存了这些session,并且还可以根据zhangsan,lisi,王五来查询
        //我们怎么知道他想发给谁?我们约定,发送的数据格式是json,,理由有一个字段代表了接收者的名字,我们用toUser来传递数据,msg代表要发送的数据
        Map map = objectMapper.readValue(payload, Map.class);
        //接收者
        String toUser = (String) map.get("toUser");
        String msg = (String) map.get("msg");
        //找到对方的会话
        WebSocketSession toSeesion = allClients.get(toUser);

        Object userName = session.getAttributes().get("name");

        if (toSeesion != null&&toSeesion.isOpen()) {
            //向另外一方主动发送数据
            toSeesion.sendMessage(new TextMessage("收到来自于" + userName + "的消息,内容是:" + msg));
        }else{
            session.sendMessage(new TextMessage("对方不在线"));
        }

    }

    protected void handlePongMessage(WebSocketSession session, PongMessage message) throws Exception {
        super.handlePongMessage(session, message);
        System.out.println("TestWebscoket中的方法handlePongMessage 执行了--->");
    }

    public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception {
        super.handleTransportError(session, exception);
        System.out.println("TestWebscoket中的方法handleTransportError 执行了--->");
    }

    public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
        super.afterConnectionClosed(session, status);
        System.out.println("TestWebscoket中的方法afterConnectionClosed 执行了--->");
    }
}
